#!/bin/bash

echo "🧪 调试uni-app项目构建过程..."

# 模拟构建一个测试项目
mkdir -p test-uniapp-debug
cd test-uniapp-debug

# 创建uni-app项目的package.json
cat > package.json << 'EOF'
{
  "name": "test-uniapp-project",
  "version": "1.0.0",
  "scripts": {
    "build:mp-weixin": "uni build -p mp-weixin",
    "dev:mp-weixin": "uni -p mp-weixin"
  },
  "dependencies": {
    "@dcloudio/uni-app": "^2.0.0",
    "@dcloudio/vue-cli-plugin-uni": "^2.0.0",
    "node-sass": "^4.14.1",
    "sass-loader": "^8.0.0"
  },
  "devDependencies": {
    "@vue/cli-service": "^4.5.0",
    "webpack": "^4.46.0"
  }
}
EOF

# 创建基本的uni-app项目结构
mkdir -p src/pages/index src/static
cat > src/pages/index/index.vue << 'EOF'
<template>
  <view class="container">
    <text>Hello UniApp</text>
  </view>
</template>

<script>
export default {
  data() {
    return {}
  }
}
</script>

<style>
.container {
  padding: 20px;
}
</style>
EOF

cat > src/main.js << 'EOF'
import Vue from 'vue'
import App from './App'
Vue.config.productionTip = false
App.mpType = 'app'
const app = new Vue({
  ...App
})
app.$mount()
EOF

cat > src/App.vue << 'EOF'
<template>
  <view id="app">
    <router-view/>
  </view>
</template>

<script>
export default {
  name: 'App'
}
</script>

<style>
#app {
  font-family: 'Avenir', Helvetica, Arial, sans-serif;
}
</style>
EOF

# 创建Dockerfile测试构建
cat > Dockerfile.debug << 'EOF'
FROM --platform=linux/amd64 node:14.21.3-alpine

WORKDIR /app

# 安装系统依赖
RUN apk add --no-cache \
    python3 \
    make \
    g++ \
    git \
    libc6-compat \
    linux-headers

# 设置环境变量
ENV PYTHON=/usr/bin/python3
ENV PYTHONPATH=/usr/bin/python3
ENV SASS_BINARY_SITE=https://npmmirror.com/mirrors/node-sass/
ENV NODE_SASS_SKIP_SASS_BINARY_DOWNLOAD_FOR_CI=1
ENV NODE_SASS_FORCE_BINARY=1

# 配置npm
RUN npm config set registry https://registry.npmmirror.com/
RUN npm config set sass_binary_site https://npmmirror.com/mirrors/node-sass/

# 创建.npmrc
RUN echo "registry=https://registry.npmmirror.com/" > .npmrc
RUN echo "sass_binary_site=https://npmmirror.com/mirrors/node-sass/" >> .npmrc

# 复制项目文件
COPY package.json ./
COPY src ./src

# 安装全局uni-app cli
RUN npm install -g @vue/cli @vue/cli-service-global

# 调试：检查是否需要安装uni-app相关工具
RUN echo "检查uni命令是否可用:" && which uni || echo "uni命令不存在"

# 尝试安装依赖
RUN echo "安装项目依赖..." && npm install --build-from-source --unsafe-perm --legacy-peer-deps --force --ignore-optional --ignore-engines

# 调试：检查安装后的状态
RUN echo "检查node_modules:" && ls -la node_modules/ | head -10
RUN echo "检查package.json scripts:" && cat package.json | grep -A 10 scripts
RUN echo "检查当前目录:" && ls -la

# 尝试构建
RUN echo "尝试执行构建命令..." && npm run build:mp-weixin || echo "构建失败，继续调试"

# 检查构建结果
RUN echo "检查构建后的目录结构:"
RUN ls -la
RUN ls -la dist/ 2>/dev/null || echo "dist目录不存在"
RUN ls -la unpackage/ 2>/dev/null || echo "unpackage目录不存在"

# 搜索所有可能的输出目录
RUN echo "搜索可能的输出目录:"
RUN find . -name "*.wxml" -o -name "*.wxss" -o -name "*.js" | head -10 || echo "未找到小程序文件"
RUN find . -type d -name "*mp*" -o -name "*weixin*" -o -name "*wx*" | head -10 || echo "未找到小程序相关目录"

CMD ["tail", "-f", "/dev/null"]
EOF

echo "✅ 创建了测试项目"
echo "🔧 构建测试Docker镜像..."
docker build -f Dockerfile.debug -t test-uniapp-debug .

if [ $? -eq 0 ]; then
    echo "✅ Docker构建成功！"
    echo "🔍 进入容器检查..."
    docker run -it --rm test-uniapp-debug sh
else
    echo "❌ Docker构建失败"
fi

# 清理
cd ..
rm -rf test-uniapp-debug
